<?php 
class Users extends Public_Controller{
	
	function __construct(){
		parent::__construct();
	}
	
	public function login(){
			
		$this->template
			->set_layout('users.html')
			->append_css('login.css');
		if(!empty($this->current_user)){
			redirect(base_url(),'refresh');
		}
		$error = NULL;
		$this->load->model('user_m');
		$model = $this->input->post();
		$this->form_validation->set_rules('username', 'lang:users:username', 'required');
		$this->form_validation->set_rules('password', 'lang:users:password', 'required');
		//if( !empty( $this->input->post() ) ){
			if ($this->form_validation->run() == TRUE)
			{
				$u = $model['username'];
				$p = $model['password'];
				$this->current_user = $this->user_m->login($u,$p);
				if($this->current_user){
					$this->load->model('emp_profile/profile_m');
					$this->current_profile = $this->profile_m->get($this->current_user->id);
					$this->session->set_userdata('current_user', $this->current_user);
					$this->session->set_userdata('current_profile', $this->current_profile);
					redirect(base_url(),'refresh');
				}
				else
				{
					$this->session->set_flashdata('error', lang('users:login:fail_1'));
				}
			}
		//}
		$this->template
			->set('model',$model)
			->set('error',$this->session->flashdata('error'))
			->set('success',$this->session->flashdata('success'))
			->append_css('error.css')
			->append_css('alert.css')
			->build($this->controller.'/users/'.$this->method);
	}
	
	public function register(){
		$this->load->model('emp_room/room_m');
		$rooms = $this->room_m->get_all_list();
		$this->template
			->set_layout('users.html')
			->append_css('login.css')
			->append_css('register.css')
			->append_css('map.css')
			->set('rooms',$rooms)
			->build($this->controller.'/users/register_step1');
	}
	public function register_step2($room_id){
		$sussess = false;
		$this->form_validation->set_rules('display_name', 'lang:users:full_name', 'required');
		$this->form_validation->set_rules('username', 'lang:users:username', 'required|max_length[20]|is_unique[ser_users.username]|trim|alpha_dash');
		$this->form_validation->set_rules('password', 'lang:users:password', 'required');
		//$this->form_validation->set_rules('email', 'Email', 'required|valid_email|trim|max_length[60]|is_unique[ser_users.email]');
		$this->form_validation->set_rules('email', 'Email', 'trim|max_length[60]');
		$this->form_validation->set_rules('birth', 'lang:users:birth', 'required');
		$this->form_validation->set_rules('address', 'lang:users:address', 'required|trim|max_length[100]');
//		$this->form_validation->set_rules('address2', 'lang:users:address2', 'required|trim|max_length[100]');
		
		$this->form_validation->set_rules('sex', 'lang:users:sex', 'required');
		$this->form_validation->set_rules('literacy', 'lang:users:literacy', 'required');
		
		
		
		$this->load->model('emp_room/room_m');
		$room = $this->room_m->get($room_id);
		$rooms = $this->room_m->get_all();
		//$room_select_format = array();
		//array_level1($rooms,$room_select_format);
		//$room_select = array_for_select($room_select_format,'id','title');
		//chuc vu lay tu model groups_m
		$this->load->model('groups_m');
		
		//$param['not_name'] = array('admin');
		//$groups = $this->groups_m->get_all($param);
		//$groups_select = array_for_select($groups,'id','description');
		
		//sex
		$this->load->model('list_m');
		$sex_select = $this->list_m->sex_select;
		
		$literacy_select = $this->list_m->literacy_select;
		$marry_select = $this->list_m->marry_select;
		
		
		$model = $this->input->post();
		if ($this->form_validation->run() == TRUE)
		{
			//tbl.ser_user
			$user['username'] = $model['username'];
			$user['email'] = $model['email'];
			$user['password'] = $model['password'];
			$user['display_name'] = $model['display_name'];
			$user['group_id'] = $model['position_id']; 
			$this->load->model('user_m');
			$id_insert = $this->user_m->insert($user);
			//tbl.emp_profile
			$profile['id'] = $id_insert;
			$profile['room_id'] = $model['room_id'];
			$profile['id_number'] = $model['id_number'];
			$profile['sex'] = $model['sex'];
			$profile['birth'] = strtotime($model['birth']);
			$profile['phone'] = $model['phone'];
			$profile['space'] = $model['space'];
			$profile['address'] = $model['address'];
			$profile['address2'] = $model['address2'];
			$profile['people'] = $model['people'];
			$profile['marry'] = $model['marry'];
			$profile['location'] = $model['location'];
			$profile['literacy'] = $model['literacy'];
			$profile['specialize'] = $model['specialize'];
			$profile['certificate'] = $model['certificate'];
			$profile['date_start'] = strtotime($model['date_start']);
			$profile['bank_id'] = $model['bank_id'];
			//$profile['room_id'] = $model['certificate'];
			//$profile['room_id'] = $model['certificate'];
			
			$this->load->model('emp_profile/profile_m');
			$this->profile_m->insert($profile);
			$sussess = true;
			$this->session->set_flashdata('success', lang('users:success_waiting'));
			
		}
		$this->template
			->set_layout('users.html')
			->append_css('login.css')
			->append_css('register.css')
			->set('room',$room)
			->set('sussess',$sussess)
			->set('model',$model)
			->set('sex_select',$sex_select)
			->set('marry_select',$marry_select)
			
			->set('literacy_select',$literacy_select)
			//js
			->append_css('ui/jquery-ui.min.css')
			->append_js('jquery.min.js')
			->append_js('ui/jquery-ui.min.js')
			->build($this->controller.'/users/register_step2');
	}
	
	public function logout(){
		$this->session->sess_destroy();
		redirect(base_url(),'refresh');
	}
	
	public function index(){
		menu_active();
		//$menu_left = $this->menu_left();
		
		$this->load->model('emp_profile/profile_m');
		
		$this->user_profile = $this->profile_m->get($this->current_user->id);
		$this->load->model('list_m');
		$sex_select = $this->list_m->sex_select;
		$marry_select = $this->list_m->marry_select;
		
		$this->load->model('groups_m');
		$param['not_name'] = array('admin');
		$groups = $this->groups_m->get_all($param);
		$groups_select = array_for_select($groups,'id','description');
		
		$this->load->model('emp_room/room_m');
		$rooms = $this->room_m->get_all();
		$room_select_format = array();
		array_level1($rooms,$room_select_format);
		$room_select = array_for_select($room_select_format,'id','title');
		
		$literacy_select = $this->list_m->literacy_select;
		
		$this->template->set_layout('body.html')
			//->set('menu_left',$menu_left)
			->set('user_profile',$this->user_profile)
			->set('sex_select',$sex_select)
			->set('marry_select',$marry_select)
			->set('groups_select',$groups_select)
			->set('room_select',$room_select)
			->set('literacy_select',$literacy_select)
			->append_css('users.css')
			->append_css(array(
				//'users.edit.css',
				'ui/jquery-ui.min.css','paper.css'
			))
			->title(lang('users:info'))
			->build($this->controller.'/index');
	}
	function menu_left(){
		return $this->template
			->set_layout(false)
			->build($this->controller.'/menu_left');
	}
	public function edit($id = NULL){
		//phan quyen
		if($id != NULL && $this->current_user->group!="admin"){
			$this->check_permissions();
		}
		$this->load->model('user_m');
		
		$user_id = !empty($id) ? $id : $this->current_user->id;
		$model = (array)$this->user_m->get($user_id);
		$model['password'] = NULL;
		$is_unique =  '';
		if($this->input->post('id2') != $model['id2']) {
		   $is_unique =  '|is_unique[emp_profile.id2]';
		}
		//validate
		//admin
		if($this->current_user->group == "admin"){
			$this->form_validation->set_rules('address', 'lang:users:address', 'required');//admin
			$this->form_validation->set_rules('display_name', 'lang:users:full_name', 'required');//admin
			$this->form_validation->set_rules('id_number', 'lang:users:id_number', 'required');//admin
			$this->form_validation->set_rules('literacy', 'lang:users:literacy', 'required');//admin
		}
		$this->form_validation->set_rules('sex', 'lang:users:sex', 'required');
		$this->form_validation->set_rules('marry', 'lang:users:marry', 'required');
		$this->form_validation->set_rules('email', 'Email', 'valid_email');
		$this->form_validation->set_rules('id2', 'lang:users:id2', 'trim'.$is_unique);
		//end validate
		$type =  substr($this->input->post('btnAction'), 0);
		if ( !empty($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$model  = $this->input->post();
				$skip = array('btnAction','id');
				foreach($skip as $obj){
					if(array_key_exists($obj,$model))
						unset($model[$obj]); 
				}
				$user['email'] = $model['email'];
				if(!empty($model['password']))
					$user['password'] = md5($model['password']);
				if($this->current_user->group == "admin"){
					$user['display_name'] = $model['display_name'];
				}
				$this->load->model('user_m');
				$this->user_m->update_user($user_id,$user);
				//tbl.emp_profile
				//admin
				if($this->current_user->group == "admin"){
					$profile['address'] = $model['address'];//admin
					$profile['literacy'] = $model['literacy'];
					$profile['bhxh'] = $model['bhxh'];
					$profile['mst'] = $model['mst'];
					$profile['id_number_date'] = $model['id_number_date'];
					$profile['id_number_space'] = $model['id_number_space'];
					$profile['id_number'] = $model['id_number'];
					$profile['date_start'] = strtotime($model['date_start']);
					$profile['allows_1'] = $model['allows_1'];
					$profile['allows_2'] = $model['allows_2'];
					$profile['limit_hd'] = $model['limit_hd'];
					$profile['id2'] = $model['id2'];
					$profile['location'] = $model['location'];
					
				}
				$profile['sex'] = $model['sex'];
				$profile['birth'] = strtotime($model['birth']);
				$profile['phone'] = $model['phone'];
				$profile['space'] = $model['space'];
				$profile['address2'] = $model['address2'];
				$profile['people'] = $model['people'];
				$profile['marry'] = $model['marry'];
				$profile['specialize'] = $model['specialize'];
				$profile['certificate'] = $model['certificate'];
				$profile['bank_id'] = $model['bank_id'];			
				
				
				
				$this->load->model('emp_profile/profile_m');
				$this->profile_m->update($user_id,$profile);
				$this->session->set_flashdata('success', lang('users:update_success'));
				//site_url($this->uri->uri_string())
				$id_redirect = !empty($id) ? $id : ""; 
				redirect('users/edit/'.$id_redirect);
				
			}
		}
		$this->load->model('list_m');
		$sex_select = $this->list_m->sex_select;
		
		$literacy_select = $this->list_m->literacy_select;
		$marry_select = $this->list_m->marry_select;
		
		$this->template
			->set('title',lang('users:change_info'))
			->set(array(
			'model' => $model,
			'sex_select' => $sex_select,
			'marry_select' => $marry_select,
			'literacy_select' => $literacy_select
			))
			->append_css(array(
				//'users.edit.css',
				'input.css','button.css','ui/jquery-ui.min.css','paper.css','error.css'
			))
			->append_js(array(
				'ui/jquery-ui.min.js'
			))
			->set_layout('body.html')
			->build($this->controller.'/edit');
	}
	
	
}